<?php
include 'class/in_it.php'; 
protected_pages();

if (empty($_POST) === false){
    $required_fields = array('currentpassword', 'password', 'r_password');
    foreach($_POST as $key=>$value){
        if (empty($value) && in_array($key, $required_fields) === true){
            $errors[] = 'Please fill all fields.';
            break 1;
        }
    }
    
    if (md5($_POST['currentpassword']) === $userdata['password']){
        if (trim($_POST['password']) !== trim($_POST['r_password'])){
            $errors[] = 'Your new passwords do not match.';
        } else if (strlen($_POST['password']) < 6) {
            $errors[] = 'Your password must be atleast 6 characters';
        }
    } else{
        $errors[] = 'Your current password is incorrect!';
    }
}


include 'includes/overall/header.php';

?>
<h1>Change Password</h1>

<?php
if (isset($_GET['success']) && empty($_GET['success'])) {
    echo 'Your password has been changed.';
} else {
    if (empty($_POST) === false && empty($errors) === true){
        changepass($session_user_id, $_POST['password']);
        header('Location: changepassword.php?success');
    } else if (empty($errors) === false){
        echo output_errors($errors);
    }
    ?>
<div class="widget"> 
	<div class="inner2">  

    <form action="" method="post">
        <ul>
            <li>
                Current password:<br>
                <input type="password" name="currentpassword">
            </li>
            <li>
                New password:<br>
                <input type="password" name="password">
            </li>
            <li>
                Repeat new password:<br>
                <input type="password" name="r_password">
            </li>
            <li>    
                <input type="submit" value="Change Password">
            </li>        
        </ul>
            </div>
</div>   
<?php
}
include 'includes/overall/footer.php';
?>
